<?php
/**
 * Config.php
 * 
 * @category    Madoqua
 * @package     Cache
 */

/** zend action */
require_once 'Zend/Cache.php';

/**
 * Madoqua_Cache_Config
 * 
 * @category    Madoqua
 * @package     Cache
 */
class Madoqua_Cache_Config 
{
    /**
     * the cache
     *
     * @var array
     */
    protected static $_caches = array();

    /**
     * get the cache
     *
     * @return Zend_Cache_Core
     */
    protected static function getCache($file)
    {
         if (empty(self::$_caches[$file])) {
            //cache doesn't exist yet
            
            $frontendOptions = array(
                'master_file' => $file,
                'automatic_serialization' => true
            );
            $backendOptions = array(
                'cache_dir' => Madoqua_Application::getDataDirectory() . DIRECTORY_SEPARATOR . 'cache/config/'
            );
            //caching options
    
            self::$_caches[$file] = Zend_Cache::factory('file', 'file', $frontendOptions, $backendOptions);
            //the cache
        }
        
        return self::$_caches[$file];
    }
    
    /**
     * get the action cache
     * 
     * @param string $configFile
     * @return Zend_Config_Xml
     */
    public static function get($configFile) {

        $cache = self::getCache($configFile);
        //the cache matching the file
        
        $id = md5($configFile);
        //id for the config
        
        $config = $cache->load($id);
        //try to load it
        
        if ($config === false) {
            //cache doesn't give us a hit

            $config = new Zend_Config_Xml($configFile, null, true);
            //we'll build it ourselves
                    
            $cache->save($config, $id);
            //and save it

        }
        return $config;
        //pass it back
    }
}